package com.evs.system.employee.controller;

import com.evs.system.department.domain.Department;
import com.evs.system.employee.service.IEmployeeService;
import com.evs.system.employee.domain.Employee;
import com.evs.system.employee.query.EmployeeQuery;
import com.evs.utils.AjaxResult;
import com.evs.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    public IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param employee  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value = "新增部门【如有id为修改操作】")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if( employee.getId()!=null)
                employeeService.update(employee);
            else
                employeeService.add(employee);
            return AjaxResult.success().setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("保存失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "根据id删除一条数据")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg("删除失败！"+e.getMessage());
        }
    }

    @PatchMapping("/del")
    @ApiOperation(value = "根据id批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.success().setMsg("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "查询部门的一条数据")
    public Employee queryById(@PathVariable("id")Long id)
    {
        return employeeService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有员工所有数据")
    public List<Employee> queryAll(){

        return employeeService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/pageList")
    @ApiOperation(value = "员工分页查询")
    public AjaxResult pageList(@RequestBody EmployeeQuery query){
        try {
            List<Employee> page = employeeService.queryPage(query);
            Long count = employeeService.queryCount(query);
            return AjaxResult.success().setData(page).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
            return AjaxResult.error().setMsg("获取分页数据失败");
    }
    @GetMapping("/export")
    public void Export(HttpServletResponse response){
        try {
            //查询全部数据
            List<Employee> employees = employeeService.queryAll();
            //使用工具类排版数据
            ExcelUtil.exportExcel(employees,"员工详细信息","员工数据",Department.class,"员工数据的详细信息.xls",response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Department> Departments = ExcelUtil.importExcel(file, 1, 1, Department.class);
        Departments.forEach(System.out::println);
    }
}
